package com.yuyou.fn.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.pingplusplus.model.Charge;
import com.yuyou.fn.settlement.service.IPayOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * Created by wens on 2017/3/6.
 */
@Controller
public class PayOrderController {

    @Resource
    private IPayOrderService payOrderService ;

    @RequestMapping("/api/pay/preparePay")
    @ResponseBody
    public ReturnResult<Charge> preparePay(@RequestParam("amount") BigDecimal amount , @RequestParam("busId") Long busId , @RequestParam("busType") String busType , @RequestParam("channel") String channel , @RequestParam("subject") String subject , @RequestParam(value = "body" , required = false ) String body , @RequestParam("extra") String extra ) {

        Map<String,String> extraMap = null ;

        if(StringUtils.isNotEmpty(extra )){
            extraMap = JSON.parseObject(extra , new TypeReference<Map<String, String>>(){}) ;
        }

        Charge charge = payOrderService.preparePay(busId ,busType , amount, channel, subject, body , extraMap );
        return ReturnResult.success(charge);
    }



    @RequestMapping("/api/pay/callback")
    @ResponseBody
    public void callback(@RequestBody String body , @RequestHeader("x-pingplusplus-signature") String signature ) {
        //System.out.println(body);
        payOrderService.callback(body,signature );


    }




}
